const ReactNative = require('react-native')
const { Buffer } = require('buffer')

const TSC = require('./js/tsc');
const ESC = require('./js/esc');
const Util = require('./js/util');

const {NativeModules, DeviceEventEmitter, Platform} = ReactNative;
const BluetoothSerial = null;
if(Platform.OS === 'ios'){
    BluetoothSerial = NativeModules.BluetoothSerial;
}else {
	BluetoothSerial = NativeModules.GMBluetooth
}
/**
 * Listen for available events
 * @param  {String} eventName Name of event one of connectionSuccess, connectionLost, data, rawData
 * @param  {Function} handler Event handler
 */
BluetoothSerial.on = (eventName, handler) => {
  DeviceEventEmitter.addListener(eventName, handler)
}

/**
 * Stop listening for event
 * @param  {String} eventName Name of event one of connectionSuccess, connectionLost, data, rawData
 * @param  {Function} handler Event handler
 */
BluetoothSerial.removeListener = (eventName, handler) => {
  DeviceEventEmitter.removeListener(eventName, handler)
}

/**
 * Write data to device, you can pass string or buffer,
 * We must convert to base64 in RN there is no way to pass buffer directly
 * @param  {Buffer|String} data
 * @return {Promise<Boolean>}
 */
BluetoothSerial.write = (data) => {
  if (typeof data === 'string') {
    data = new Buffer(data)
  }
  return BluetoothSerial.writeToDevice(data.toString('base64'))
}

BluetoothSerial.TSC = TSC;
BluetoothSerial.ESC = ESC;
BluetoothSerial.Util = Util;

ESC._setBT(BluetoothSerial);
TSC._setBT(BluetoothSerial);

module.exports = BluetoothSerial
